Java code critique request [closed]

Posted by davidk01 on Programmers See other posts from Programmers or by davidk01
Published on 2011-02-06T03:08:27Z Indexed on 2011/02/06 7:33 UTC
Read the original article Hit count: 632

Can you make sense of the following bit of java code and do you have any suggestions for improving it? Instead of writing four almost identical setOnClickListener method calls I opted to iterate over an array but I'm wondering if this was the best way to do it. Here's the code:

    /* Set up the radio button click listeners so two categories are not selected
       at the same time. When one of them is clicked it clears the others.
    */
    final RadioButton[] buttons = {radio_books,radio_games,radio_dvds,radio_electronics};
    for (int i = 0; i < 4; i++) {
        final int k = i;
        buttons[i].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int j = 0; j < 4; j++) {
                    if (buttons[j] != buttons[k]) {
                        buttons[j].setChecked(false);
                    }
                }
            }
        });
    }

© Programmers or respective owner

Related posts about java

Related posts about code-reviews